﻿//using System;
//using System.Collections.Generic;
//using System.IO;
//using System.Linq;
//using System.Reflection;
//using System.Threading.Tasks;
//using Volo.Abp.Domain.Entities;
//using Volo.Abp.Domain.Repositories;

//namespace Xms.Abp.Domain.Repositories;

//public static class IRepositoryExtensions
//{
//    private static readonly Func<IQueryable, Type, IQueryable> _AsNoTrackingFunc;
//    static IRepositoryExtensions()
//    {
//        var type = Assembly.LoadFrom(Path.Combine(AppContext.BaseDirectory, "Microsoft.EntityFrameworkCore.dll")).GetType("Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions");
//        var method = type.GetMethod(nameof(AsNoTracking), BindingFlags.Public | BindingFlags.Static);
//        _AsNoTrackingFunc = (queryable, type) => method.MakeGenericMethod(type).Invoke(null, new object[] { queryable }) as IQueryable;
//    }
//    public static IQueryable<TEntity> AsNoTracking<TEntity>(this IQueryable<TEntity> queryable)
//        where TEntity : class, IEntity
//    {
//        try
//        {
//            return _AsNoTrackingFunc(queryable, typeof(TEntity)) as IQueryable<TEntity>;
//        }
//        catch
//        {
//            return queryable;
//        }
//    }

//    public static async Task AttachAsync<TEntity>(this IRepository<TEntity> repository, TEntity entity)
//        where TEntity : class, IEntity
//    {
//        if (repository is RepositoryBase<TEntity> repo)
//        {
//            var provider = repo.LazyServiceProvider.LazyGetRequiredService<IAttachProvider>();
//            await provider.AttachAsync(repository, entity);
//        }
//    }

//    public static async Task AttachAsync<TEntity>(this IRepository<TEntity> repository, IEnumerable<TEntity> entities)
//        where TEntity : class, IEntity
//    {
//        if (repository is RepositoryBase<TEntity> repo)
//        {
//            var provider = repo.LazyServiceProvider.LazyGetRequiredService<IAttachProvider>();
//            await provider.AttachAsync(repository, entities);
//        }
//    }
//}

